package com.tubruk.trackme;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.location.Address;
import android.location.Geocoder;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;

public class MyLocationGestureListener extends Overlay implements OnGestureListener {

	private MyLocationOverlay loc;
	private MapView mapView;
	private GestureDetector gestureDetector;

	private MapController mc;
	
	public MyLocationGestureListener(MapController mc){
		this.mc = mc;
		gestureDetector = new GestureDetector(this);	
		loc = new MyLocationOverlay();
	}
	
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
//		loading(e, this.mapView);
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		loading(e, this.mapView);
		loadAddressFromEvent(e, this.mapView);
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	private void loading(MotionEvent event, MapView mapView) {
		GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
				(int) event.getY());

		Geocoder geoCoder = new Geocoder(TrackMapActivity.getContext(), Locale
				.getDefault());
					
			loc.setParameter(p, "loading...");
			mapView.getOverlays().add(loc);
		
			//mapView.postInvalidate();
			mapView.invalidate();
	}
	private void loadAddressFromEvent(MotionEvent event, MapView mapView) {
		GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
				(int) event.getY());

		Geocoder geoCoder = new Geocoder(TrackMapActivity.getContext(), Locale
				.getDefault());
		
		try {
			List<Address> addresses = geoCoder.getFromLocation(p
					.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);

			String add = "";
			if (addresses.size() > 0) {
				int size = addresses.get(0).getMaxAddressLineIndex();
				if (size > 0) {
					add += addresses.get(0).getAddressLine(0);
					for (int i = 1; i < size; i++) {
						add += ", " + addresses.get(0).getAddressLine(i);
					}
				}
			}
			List<Overlay> overlays = mapView.getOverlays();
			int size = overlays.size();
			for (int i =0;i<size;i++){
				Overlay overlay = overlays.get(i);
				if (overlay.equals(loc)){
					overlays.remove(i);
					i = size;
				}
			}
					
			loc.setParameter(p, add);
			mapView.getOverlays().add(loc);
			
			mc.animateTo(p);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		this.mapView = mapView;
		if (gestureDetector.onTouchEvent(event)) {
			return true;
		}
		return false;
    }
}
